package com.agile.major.web.rest;

import com.agile.major.domain.BaseStudentInfo;
import com.agile.major.service.BaseStudentInfoService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * REST controller for managing BaseStudentInfo.
 */
@RestController
@RequestMapping("/api")
public class BaseStudentInfoResource {

    private final Logger log = LoggerFactory.getLogger(BaseStudentInfoResource.class);

    private static final String ENTITY_NAME = "baseStudentInfo";

    private final BaseStudentInfoService baseStudentInfoService;

    public BaseStudentInfoResource(BaseStudentInfoService baseStudentInfoService) {
        this.baseStudentInfoService = baseStudentInfoService;
    }

    /**
     * POST  /base-student-infos : Create a new baseStudentInfo.
     *
     * @param baseStudentInfo the baseStudentInfo to create
     * @return the ResponseEntity with status 201 (Created) and with body the new baseStudentInfo, or with status 400 (Bad Request) if the baseStudentInfo has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/base-student-infos")
    @Timed
    public ResponseEntity<BaseStudentInfo> createBaseStudentInfo(@RequestBody BaseStudentInfo baseStudentInfo) throws URISyntaxException {
        log.debug("REST request to save BaseStudentInfo : {}", baseStudentInfo);
        if (baseStudentInfo.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new baseStudentInfo cannot already have an ID")).body(null);
        }
        BaseStudentInfo result = baseStudentInfoService.save(baseStudentInfo);
        return ResponseEntity.created(new URI("/api/base-student-infos/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /base-student-infos : Updates an existing baseStudentInfo.
     *
     * @param baseStudentInfo the baseStudentInfo to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated baseStudentInfo,
     * or with status 400 (Bad Request) if the baseStudentInfo is not valid,
     * or with status 500 (Internal Server Error) if the baseStudentInfo couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/base-student-infos")
    @Timed
    public ResponseEntity<BaseStudentInfo> updateBaseStudentInfo(@RequestBody BaseStudentInfo baseStudentInfo) throws URISyntaxException {
        log.debug("REST request to update BaseStudentInfo : {}", baseStudentInfo);
        if (baseStudentInfo.getId() == null) {
            return createBaseStudentInfo(baseStudentInfo);
        }
        BaseStudentInfo result = baseStudentInfoService.save(baseStudentInfo);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, baseStudentInfo.getId().toString()))
            .body(result);
    }

    /**
     * GET  /base-student-infos : get all the baseStudentInfos.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of baseStudentInfos in body
     */
    @GetMapping("/base-student-infos")
    @Timed
    public ResponseEntity<List<BaseStudentInfo>> getAllBaseStudentInfos(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of BaseStudentInfos");
        Page<BaseStudentInfo> page = baseStudentInfoService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/base-student-infos");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /base-student-infos/:id : get the "id" baseStudentInfo.
     *
     * @param id the id of the baseStudentInfo to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the baseStudentInfo, or with status 404 (Not Found)
     */
    @GetMapping("/base-student-infos/{id}")
    @Timed
    public ResponseEntity<BaseStudentInfo> getBaseStudentInfo(@PathVariable Long id) {
        log.debug("REST request to get BaseStudentInfo : {}", id);
        BaseStudentInfo baseStudentInfo = baseStudentInfoService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(baseStudentInfo));
    }

    /**
     * DELETE  /base-student-infos/:id : delete the "id" baseStudentInfo.
     *
     * @param id the id of the baseStudentInfo to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/base-student-infos/{id}")
    @Timed
    public ResponseEntity<Void> deleteBaseStudentInfo(@PathVariable Long id) {
        log.debug("REST request to delete BaseStudentInfo : {}", id);
        baseStudentInfoService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    @GetMapping("/base-student-infos/stu-total/{baseMajorId}/{year}")
    @Timed
    public ResponseEntity getGraduateInfo(@PathVariable Long baseMajorId,@PathVariable String year) {
        Map result = baseStudentInfoService.getStuCount(baseMajorId,year);
        // return new ResponseEntity(result, HttpStatus.OK);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(result));
    }
}
